package com.ky.pro.java.fx.spring.boot.starter;

import com.ky.pro.java.fx.spring.boot.starter.properties.SchoolProperties;
import com.ky.pro.java.fx.spring.boot.starter.properties.SchoolsProperties;
import com.ky.pro.java.fx.spring.entity.Klass;
import com.ky.pro.java.fx.spring.entity.School;
import com.ky.pro.java.fx.spring.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName SchoolConfiguration
 * @Description TODO
 * @Date 2021/12/4 16:53
 * @Author kevin
 **/
@Configuration
public class SchoolConfiguration {

    @Autowired
    SchoolsProperties schoolsProperties;

    @Bean("schools")
    List<School> genSchools() {
        return schoolsProperties.getList().stream().map(schoolProp->{
            School school = new School();
            school.setName(schoolProp.getName());
            school.setId(schoolProp.getId());
            school.setKlasses(schoolProp.getKlasses().stream().map(klassProp->{
                Klass klass = new Klass();
                klass.setName(klassProp.getName());
                klass.setId(klassProp.getId());
                klass.setStudents(klassProp.getStudents().stream().map(studentProp->{
                    Student student = new Student();
                    student.setName(studentProp.getName());
                    student.setId(studentProp.getId());
                    student.setAge(studentProp.getAge());
                    student.setGender(studentProp.getGender());
                    return student;
                }).collect(Collectors.toSet()));
                return klass;
            }).collect(Collectors.toSet()));
            return school;
        }).collect(Collectors.toList());
    }

}
